package com.android.pindaojia.fragment.group;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.pindaojia.fragment.BaseLazyFragment;
import com.android.pindaojia.fragment.detail.Fragment_BDMain_TabList;
import com.android.pindaojia.model.group.BrandDiscountMainData;
import com.android.pindaojia.utils.ConstantsUrl;
import com.android.pindaojia.utils.ObjectUtils;
import com.android.pindaojia.R;
import com.android.pindaojia.model.group.BrandDiscountMainDataCallback;
import com.android.pindaojia.utils.HttpUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/6/29.
 */

public class Fragment_BrandDiscountMain extends BaseLazyFragment implements BaseQuickAdapter.RequestLoadMoreListener{
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.tab)
    TabLayout tabLayout;
    @BindView(R.id.pager)
    ViewPager viewPager;
    private static final String ARG_PARAM1 = "param1";
    private LayoutInflater inflater;
    private View view;
    private FragmentActivity mcontext;
    private boolean networkConnected;
    private GroupFragmentPagerAdapters pagerAdapter;
    private StaggeredGridLayoutManager mLayoutManager;
    private ArrayList<BrandDiscountMainData.DataBean.Cat_list> cat_list = new ArrayList<>();
    private int page=1;
    public static Fragment_BrandDiscountMain newInstance() {
        Fragment_BrandDiscountMain fragment = new Fragment_BrandDiscountMain();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }
    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_branddiscountmain, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }
    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {

        GetBDMain();

    }
    @Override
    public void onLoadMoreRequested() {
        page = page + 1;
        GetBDMain();
    }

    private  void initUI()
    {

    }
    private class GroupFragmentPagerAdapters extends FragmentPagerAdapter {

        public GroupFragmentPagerAdapters(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

                 return Fragment_BDMain_TabList.newInstance(position,cat_list.get(position).getId());
        }

        @Override
        public int getCount() {
            return cat_list.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return cat_list.get(position).getCatname();
        }
    }
    private void GetBDMain()
    {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.BRANDDISCOUNTMAIN ;
        HashMap<String,String> map = new HashMap<>();
        map.put("page",page+"");
        if (networkConnected) {

            HttpUtils.get(address,map, new BrandDiscountMainDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(BrandDiscountMainData response, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);

                    if (response.getCode() == 0) {
                        if (response.getData().getList() == null && page == 1) {
                            noData.setVisibility(View.VISIBLE);
                            return;
                        }
                        if (response.getData().getCat_list() != null) {
                            cat_list.clear();
                            cat_list.add(new BrandDiscountMainData.DataBean.Cat_list("0","全部"));
                            cat_list.addAll(response.getData().getCat_list());
                            if ( response.getData().getList() != null && response.getData().getList().size() > 0) {
                                pagerAdapter = new GroupFragmentPagerAdapters(mcontext.getSupportFragmentManager());
                                viewPager.setAdapter(pagerAdapter);
                                tabLayout.setupWithViewPager(viewPager);
                            }
                        }
                    }else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                    }
                }
            });

        } else{

            if (page == 1) {
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(mcontext, "网络未连接");
        }
    }
}
